"""
Base.py：     # 存放公共方法
打开浏览器
输入网址
浏览器的操作：后退、前进、刷新
元素定位：
元素操作：点击、输入/清空、获取元素属性.....
一般是专人维护的
"""
from pymysql.cursors import DictCursor
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pymysql
from selenium.webdriver.support.select import Select


def open_browser(browser: str = "chrome"):
    """
    打开浏览器的方法
    :param browser: 打开对应名称浏览器
    :return:
    """
    if browser.lower() == "chrome":
        return webdriver.Chrome()   # 打开谷歌浏览器
    elif browser.lower() == "firefox":
        return webdriver.Firefox()    # 打开火狐浏览器
    elif browser.lower() == "edge":
        return webdriver.Edge()    # 打开edge浏览器
    else:
        print("您老人家输入的浏览器名称有误")
        return None

class Base(object):
    def __init__(self,driver):
        self.driver = driver

    def open_url(self,url: str):
        """
        打开对应的网站
        :param url: 网站地址
        :return:
        """
        try:
            if url.startswith("http://"):
                self.driver.get(url)
            else:
                new_url = "http://"+url
                self.driver.get(new_url)
        except:
            print("您输入的网站有误")

    def find_element(self,locator: tuple,timeout=10):
        """
        元素的定位方法
        :param locator: 定位器(id,"kw")
        :param timeout: 超时时间
        :return: 元素本身
        """
        try:
            return WebDriverWait(self.driver,timeout).until(EC.presence_of_element_located(locator),message="元素未找到")
        except:
            print("元素定位失败")
            return False

    def click(self,locator: tuple,timeout=10):
        """
        对元素进行点击操作
        :param locator: 定位器
        :param timeout: 超时时间
        :return:
        """
        element = self.find_element(locator=locator,timeout=timeout)
        if element is not False:
            element.click()
        else:
            print("元素未找到，无法点击")

    def send_keys(self,locator: tuple,text,timeout=10):
        """
        对元素进行输入操作
        :param locator: 定位器
        :param text: 输入的文本
        :param timeout: 超时时间
        :return:
        """
        element = self.find_element(locator=locator, timeout=timeout)
        if element is not False:
            element.send_keys(text)
        else:
            print("元素未找到，无法输入")

    def close_browser(self):
        """
        关闭浏览器
        :return:
        """
        self.driver.quit()

    def get_element_text(self,locator: tuple,timeout=10):
        """
        获取文本值的方法
        :param locator: 定位器
        :param timeout: 超时时间
        :return: 返回元素的文本
        """
        element = self.find_element(locator=locator,timeout=timeout)
        if element is not False:
            return element.text
        else:
            return False

    def get_element_text_boolean(self, locator: tuple, timeout=10):
        """
        获取文本值的方法
        :param locator: 定位器
        :param timeout: 超时时间
        :return: 返回元素的文本
        """
        element = self.find_element(locator=locator, timeout=timeout)
        if element is not False:
            return 1
        else:
            return 0

    def select_mysql(self,maysql_password,sql):
        """
        查询数据库
        :return:
        """
        link = pymysql.connect(host='localhost', port=3306, user='root', password=maysql_password, database='ecshop_db',
                               charset='utf8')
        cur = link.cursor()
        num = cur.execute(sql)
        cur.close()
        link.close()
        return num
        
    def select_mysql_num(self, maysql_password, sql):
        """
        查询数据库
        :return:查询结果
        """
        link = pymysql.connect(host='localhost', port=3306, user='root', password=maysql_password, database='ecshop_db',
                               charset='utf8')
        cur = link.cursor()
        cur.execute(sql)
        num = cur.fetchall()
        num = int(num[0][0])
        cur.close()
        link.close()
        return num
        
    def select_dropdown(self,locater,value):
        """
        选择下拉项
        :return:
        """
        element = self.find_element(locater)
        Select(element).select_by_visible_text(value)

    def clear_box(self,locator: tuple,timeout=10):
        """
        清空输入框内容
        :return:
        """
        element = self.find_element(locator=locator,timeout=timeout)
        element.clear()

    def pop_up_notification(self):
        """
        弹窗点击确定
        :return:
        """
        self.driver.switch_to.alert.accept()
        
    def select_mysql_data(self, maysql_password, sql):
        """
        查询数据库
        :return:查询结果
        """
        link = pymysql.connect(host='localhost', port=3306, user='root', password=maysql_password, database='ecshop_db',
                               charset='utf8')
        cur = link.cursor(cursor=DictCursor)
        cur.execute(sql)
        data = cur.fetchall()
        cur.close()
        link.close()
        return data

    def select_mysql_date(self, maysql_password, sql):
        """
        连接数据库,查询商品的件数
        :return:购买的件数
        """
        link = pymysql.connect(host='localhost', port=3306, user='root', password=maysql_password, database='ecshop_db',
                               charset='utf8')
        cur = link.cursor()
        cur.execute(sql)
        data = cur.fetchall()
        data1 = str(data[0][0])
        cur.close()
        link.close()
        return data1

    def clear(self, locator: tuple, timeout=10):
        """
        清空文本框内容
        :param locator:
        :param timeout:
        :return:
        """
        element = self.find_element(locator=locator, timeout=timeout)
        if element is not False:
            element.clear()
        else:
            print("元素未找到，无法点击")

    def frame_open(self, locator: tuple, timeout=10):
        """
        进入frame
        :param locator:
        :param timeout:
        :return:
        """
        element = self.find_element(locator=locator, timeout=timeout)
        if element is not False:
            self.driver.switch_to.frame(element)
        else:
            print("元素未找到，无法点击")

    def frame_off(self):
        """
        frame返回上一层
        :return:
        """
        self.driver.switch_to.parent_frame()

